package com.cloud.wms.ui.inside.replenish;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Message;

import com.cloud.core.config.EventConfig;
import com.cloud.core.eventBus.Event;
import com.cloud.widget.ScanText;
import com.cloud.wms.R;
import com.cloud.wms.base.BaseBarActivity;
import com.cloud.wms.config.IntentKey;
import com.cloud.wms.ui.inside.contract.ReplenishApplyContact;
import com.cloud.wms.ui.inside.entity.RecordGoodsEntity;
import com.cloud.wms.ui.inside.presenter.ReplenishApplyPresenter;
import com.cloud.wms.widget.dialog.AlertDialogBuilder;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;

/**
 * Created by FC on 2021/3/12.
 * Desc：补货申请扫描商品条码
 */
public class ApplyCommodityActivity extends BaseBarActivity<ReplenishApplyPresenter> implements ReplenishApplyContact.View {
    @BindView(R.id.scan_text)
    ScanText scanText;
    private String posCode;

    @Override
    protected String barTitle() {
        return "补货申请";
    }

    @Override
    protected int getResId() {
        return R.layout.activity_replenish_apply_start;
    }

    @Override
    protected void onScanHandle(String result, int resultCode) {
        super.onScanHandle(result, resultCode);
        scanText.addHistory(result);
        new AlertDialogBuilder(this)
                .setTitle("提示")
                .setMessage("请选择良次品")
                .setPositiveButton("良品", (dialog, which) -> {
                    mPresenter.getPosCommodityMsg(posCode, result, 1);
                })
                .setNeutralButton("次品", (dialog, which) -> {
                    mPresenter.getPosCommodityMsg(posCode, result, 2);
                })
                .show();
    }

    @Override
    protected void initEvent() {
        initScanText(scanText);
        scanText.setHint("请输入或扫描条码");
        posCode = getIntent().getStringExtra(IntentKey.INFO_KEY);
    }

    @Override
    public void onSuccess(Message message) {
        if (message.what == ReplenishApplyPresenter.GET_POS_COMMODITY_MSG) {
            RecordGoodsEntity goodsEntity = (RecordGoodsEntity) message.obj;
            jump(goodsEntity);
        }
    }

    private void jump(RecordGoodsEntity goodsEntity) {
        Intent intent = new Intent(this, ApplyConfirmActivity.class);
        intent.putExtra(IntentKey.OBJECT_KEY, goodsEntity);
        startActivity(intent);
    }

    @Override
    protected boolean isUseEventBus() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.POSTING)
    public void onEventCome(Event event) {
        if (event == null) {
            return;
        }
        switch (event.getCode()) {
            case EventConfig.ADD_RECORD_SUCCESS:  //添加补货记录成功
                finish();
                break;
        }
    }

    @Override
    protected void setupComponent() {
        getActivityComponent().inject(this);
    }
}
